package com.zpz.framework.zpzoauth.service.impl;

import com.zpz.framework.zpzoauth.common.encrypt.SignUtil;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultMsg;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultStatus;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthRequestUtil;
import com.zpz.framework.zpzoauth.dao.ZpzOauthOauthClientDetailsMapper;
import com.zpz.framework.zpzoauth.pojo.po.OauthClientDetails;
import com.zpz.framework.zpzoauth.pojo.qo.TraceQo;
import com.zpz.framework.zpzoauth.pojo.vo.TraceVo;
import com.zpz.framework.zpzoauth.service.ZpzOauthOauthClientDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangpanzhi
 * @description 客户端信息管理服务
 * @create 2019-08-07 10:42
 */
@Service
public class ZpzOauthOauthClientDetailsServiceImpl implements ZpzOauthOauthClientDetailsService {

    @Resource
    private ZpzOauthOauthClientDetailsMapper zpzOauthOauthClientDetailsMapper;
    @Resource
    private ZpzOauthRequestUtil zpzOauthRequestUtil;
    /**
     * 获取客户端ID信息集合
     * */
    @Override
    public ZpzOauthResultMsg<List<String>> getClientDetails(HttpServletRequest request) throws Exception{
        OauthClientDetails objWhere=new OauthClientDetails();
        objWhere.setDelFlag(0);
        List<OauthClientDetails> list=zpzOauthOauthClientDetailsMapper.select(objWhere);
        List<String> retData=null;
        if (list!=null && list.size()>0){
            retData=list.stream().map(OauthClientDetails::getClientId).collect(Collectors.toList());
        }
        return ZpzOauthResultMsg.okForData(retData);
    }
    /***
     * private String d;// 私钥第三段
     *     private String v;
     *     private String z;// 公钥第三段
     *     private String q;
     *     private String b;// 私钥第一段
     *     private String p;
     *     private String y;// 公钥第二段
     *     private String jQuery;
     *     private String j;// 私钥第二段
     *     private String o;
     *     private String g;// 公钥第一段
     *     private String h;// 客户端秘钥第一段
     *     private String m;// 客户端秘钥第二段
     * */
    @Override
    public ZpzOauthResultMsg<TraceVo> trace(TraceQo traceQo) {
        OauthClientDetails client = zpzOauthRequestUtil.getClient(traceQo.getClientId());
        if (client == null){
            ZpzOauthResultMsg<TraceVo> er = ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.EXCEPTION);
            er.setMessage("无效的客户端");
            return er;
        }
//        String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJamqfpILTV5ql/+ZscBeSz7HgdcrkJFMoacY13WR+UJaRNWH5BHgbwT5VttfGx6WzEJmXkDNPqy5wOwlIkYodrrBQwEMsjvtf3lO0gUIatzeWg6xMMucCLdUbsLtw1CdLulTpOvqVyCvf0LLyvNvcE+pP0iEHLU6JhgL3DlZmwrAgMBAAECgYAvn13KNXi9F8nI8JWywP96PaOfdifL4S34qYUry6oMd+4UY7w0krYrKyrsvO6BKCidlshCHcZonZczbmV19ShsnBQbEhoeq+XqzJG5Dls4/IPZR5L2W+98BIBH6NYdJaFwzaxFct3MBsobu30stPFiz0uxE+kPxvteBjFktRCLQQJBANXYdXzEJPzmlX10mLrFSm4hv5zQaHaPlgDFpnu74VQY5TwNGhZ+x++ztZb94LQRIonzYYW/adwC/qO7FPPzPaECQQC0WSU/9PnnT5c7fplNnkZO6wS4izWfsklE1ir21y0Fi6qHsKyU40T5xkv7RKEwqDx75RH1YlOL1QrFZVWS2p5LAkEAtHmgfhHT3ZSFKYITu9zxugzFeATzhMwdnr0wilDAFIeGP64XDFEnUAdtA+dzksS7T42WCpjHMKVeXrRvNItewQJBALMDebIOXLB5lEf91fXxkSBsr/buV3/43DlzzJnP5GSIyfB+MhBntuOnLWxwJv/BPqhMLJNB0mQOR+7AMzzdvBkCQBTlUKKDeazfJ2rC9+exXDCb4P+8GvQglUbozG/SzG++yJB+x3t/UKPBoH6EwyPGyyHiZuO4y9Y0YaqVkuhITkA=";//client.getPrivateKey();
//        String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCWpqn6SC01eapf/mbHAXks+x4HXK5CRTKGnGNd1kflCWkTVh+QR4G8E+VbbXxselsxCZl5AzT6sucDsJSJGKHa6wUMBDLI77X95TtIFCGrc3loOsTDLnAi3VG7C7cNQnS7pU6Tr6lcgr39Cy8rzb3BPqT9IhBy1OiYYC9w5WZsKwIDAQAB";//client.getPublicKey();
//        String clientSecretOpen = "emN5eWdsX3dlYl8wdTh0eTRqN28=";//client.getClientSecretOpen();
        String privateKey = client.getPrivateKey();
        String publicKey = client.getPublicKey();
        String clientSecretOpen = client.getClientSecretOpen();
        String signKey = client.getSignKey();
        String pr1str = SignUtil.generateNonceStr();//随机字符串
        String pr1 = privateKey.substring(0, 55)+pr1str;
        String pr2str = SignUtil.generateNonceStr();//随机字符串
        String pr2 = pr2str+privateKey.substring(55, 95);
        String pr3str = SignUtil.generateNonceStr();//随机字符串
        String pr3 = privateKey.substring(95, privateKey.length())+pr3str;
        String pu1str = SignUtil.generateNonceStr();//随机字符串
        String pu1 = pu1str+publicKey.substring(0, 35);
        String pu2str = SignUtil.generateNonceStr();//随机字符串
        String pu2 = publicKey.substring(35, 75)+pu2str;
        String pu3str = SignUtil.generateNonceStr();//随机字符串
        String pu3 = pu3str+publicKey.substring(75, publicKey.length());
        String se1str = SignUtil.generateNonceStr();//随机字符串
        String se1 = clientSecretOpen.substring(0, 15)+se1str;
        String se2str = SignUtil.generateNonceStr();//随机字符串
        String se2 = se2str+clientSecretOpen.substring(15, clientSecretOpen.length());
        // 签名秘钥处理
        String sign1str = SignUtil.generateNonceStr();//随机字符串
        String sign1 = signKey.substring(0, 15)+sign1str;
        String sign2str = SignUtil.generateNonceStr();//随机字符串
        String sign2 = sign2str+signKey.substring(15, signKey.length());
        String jq="jQuery(){var a0oi98rt=uieyru+"+SignUtil.generateNonceStr()+"}";
        TraceVo tv=new TraceVo(pr3,SignUtil.generateNonceStr(),pu3,SignUtil.generateNonceStr(),pr1,SignUtil.generateNonceStr(),pu2,jq,pr2,SignUtil.generateNonceStr(),pu1,se1,se2,sign2,sign1);
        return ZpzOauthResultMsg.okForData(tv);
    }

}
